package com.wd.car.activity

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.wd.buycar.view.fragment.BuyFragment
import com.wd.car.R
import com.wd.car.databinding.ActivityMainBinding
import com.wd.common.base.BaseActivity
import com.wd.home.framgent.HomeFragment
import com.wd.lookcar.fragment.LookFragment
import com.wd.mine.fragment.MineFragment
import com.wd.rim.fragment.RimFragment
class MainActivity : BaseActivity<ActivityMainBinding, Any?>() {

    override fun getViewBinding() = ActivityMainBinding.inflate(layoutInflater)

    override fun initData() {
        binding.homePager.apply {
            adapter=ScreenSlidePagerAdapter(supportFragmentManager,lifecycle)
            isUserInputEnabled=false
0        }
        binding.radioGroup.apply {
            check(R.id.tab_one)
            setOnCheckedChangeListener { group, checkedId ->
                when (checkedId) {
                    R.id.tab_one ->
                        binding.homePager.setCurrentItem(0,false)//取消点击底部导航按钮时，viewPager2的滑动动画效果

                    R.id.tab_two ->
                        binding.homePager.setCurrentItem(1,false)

                    R.id.tab_three ->
                        binding.homePager.setCurrentItem(2,false)

                    R.id.tab_four ->
                        binding.homePager.setCurrentItem(3,false)

                    R.id.tab_five ->
                        binding.homePager.setCurrentItem(4,false)
                }
            }
        }
        //方案1 ：
//        binding.homePager.offscreenPageLimit = 2
    }
    private inner class ScreenSlidePagerAdapter(fa: FragmentManager, lc: Lifecycle) :
        FragmentStateAdapter(fa, lc) {
        val list = mutableListOf(
            HomeFragment(),
            RimFragment(),
            LookFragment(),
            BuyFragment(),
            MineFragment()
        )

        //方案3 ：
        override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
            super.onAttachedToRecyclerView(recyclerView)
            // 设置recyclerView itemCacheSize
            recyclerView.setItemViewCacheSize(itemCount)
        }


        override fun getItemCount(): Int = list.size

        override fun createFragment(position: Int): Fragment {
            return list[position]
        }
    }


//    方案2：
//    private class ScreenSlidePagerAdapter(fa: FragmentManager, lc: Lifecycle) :
//        FragmentStateAdapter(fa, lc) {
//        override fun getItemCount() = 5
//
//        override fun createFragment(position: Int): Fragment =
//            when(position){
//                0 ->  HomeFragment()
//                1 ->  RimFragment()
//                2 ->  LookFragment()
//                3 ->  BuyFragment()
//                else -> MineFragment()
//            }
//    }

}